package com.qianfeng.listviewdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.List;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private ListView listView;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        listView = (ListView) findViewById(R.id.listview);
//        myAdapter = new MyAdapter(this);
//        myAdapter.setNum(100);
//        listView.setAdapter(myAdapter);
//        listView.setOnItemClickListener(this);
        //   handler.sendEmptyMessageDelayed(1, 3000);
        MyFragment m = new MyFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.container, m).commit();
    }
//    private Handler handler=new Handler(){
//
//        @Override
//        public void handleMessage(Message msg) {
//            myAdapter.setNum(0);
//            myAdapter.notifyDataSetChanged();
//        }
//    };

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, Main2Activity.class);
        intent.putExtra("po", position);
        startActivityForResult(intent, 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
//            String string = data.getStringExtra("data");
//            int po = data.getIntExtra("po", -1);
//            if (po != -1) {
//                TextView viewWithTag = (TextView) listView.findViewWithTag("tv" + po);
//                if (viewWithTag != null) {
//                    viewWithTag.setText(string);
        //在此更新数据源 但是不要刷新界面
//                }
//            }
//
//        }
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        for (Fragment fragment : fragments) {
            if (fragment instanceof MyFragment) {
                fragment.onActivityResult(requestCode, resultCode, data);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
